﻿/***************************************************** 
** 命名空间: ShieldSimulatorWebBLL 
** 文 件 名：DataManager 
** 内容简述：数据管理器，负责数据的增删改查
** 版    本：V1.0 
** 创 建 人：伍兆祥
** 创建日期：2018/4/9 13:26:32 
** 修改记录：无
日期        版本      修改人    修改内容    
*****************************************************/


using System;
using System.Collections.Generic;
using ShieldSimulatorWebModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using ShieldSimulatorWebDAL;
namespace ShieldSimulatorWebBLL
{
    /// <summary>
    /// 数据中心 --删改查
    /// </summary>
    public class DataManager
    {
        /// <summary>
        /// 单例 数据中心 数据管理器 不存数据，负责和dal交互各种增删改查操作
        /// </summary>
        private static DataManager _dataManager;
        public static DataManager GetInstance()
        {
            if (_dataManager == null) return new DataManager();
            else return _dataManager;
        }
        /// <summary>
        /// 实例化一个持久层，调用其中添加数据到静态对象list<>的方法
        /// </summary>
        private DataDAL _datadal = new DataDAL();
        /// <summary>
        /// 向持久层中添加数据
        /// </summary>
        /// <param name="model">传入的simdata一份，由采集器传来</param>
        /// <returns>持久层反馈的数据存储状态，true标识成功，false标识失败</returns>
        public bool AddDataToDal(SimDataModel model)
        {
            return _datadal.AddData(model);
        }

        /// <summary>
        /// 获取Number条最新数据
        /// </summary>
        /// <param name="Number">条目数</param>
        /// <returns>最新list</returns>
        public List<SimDataModel> GetLastestData(int Number)
        {
            var list = _datadal.GetLastestDate(Number);
            return list;
        }


        /// <summary>
        /// 获取时间段内的数据
        /// </summary>
        /// <param name="StartTime">起始时间</param>
        /// <param name="EndTime">结束时间</param>
        /// <returns></returns>
        public List<SimDataModel> GetSectionData(DateTime StartTime, DateTime EndTime)
        {
            var list = _datadal.GetSectionDataByRange(StartTime, EndTime);
            return list;
        }

        /// <summary>
        /// 根据时间段来返回一段历史数据，以第一个数据所在分钟作为第一个时间段点，计算每个自然分钟内各数据 最大、最小、平均值
        /// </summary>
        /// <param name="StartTime">起始时间</param>
        /// <param name="EndTime">结束时间</param>
        /// <returns></returns>
        public List<HistoryDataModel> GetHistoryData(DateTime StartTime, DateTime EndTime)
        {
            var oldlist = _datadal.GetSectionDataByRange(StartTime, EndTime);
            var templist = new List<ForBarSimDataModel>();
            foreach (var p in oldlist)
            {
                var data = new ForBarSimDataModel()
                {
                    Id = p.Id,
                    Circle = p.Circle,
                    Temperature = p.Temperature,
                    GroundPressure = p.GroundPressure,
                    Flow = p.Flow,
                    GenerateMinute = p.GenerateTime.ToString("yyyy-MM-dd HH:mm")
                };
                templist.Add(data);
            };
            //此处得到的是分组集合数据，按照自然每分钟分组，也就是按照相同GenerateMinute分组形成集合，然后进行数据分析
            var grouplist = from ForBarSimDataModel in templist
                            group ForBarSimDataModel by ForBarSimDataModel.GenerateMinute;
            var list = new List<HistoryDataModel>();
            foreach (var p in grouplist)
            {
                //此处p即为每个分组集合的详细子分组数据，相当于一个个的list的感觉
                //而这下面的t指的是p中子元素ForBarSimData的一个个实例
                //实际上就是funcxx(ForBarSimDataModel t){return t.GroundPressure;}
                var data = new HistoryDataModel();
                data.MinGroundPressure = p.Min(t => t.GroundPressure);
                data.MaxGroundPressure = p.Max(t => t.GroundPressure);
                data.AvgGroundPressure = p.Average(t => t.GroundPressure);
                data.MinTemperature = p.Min(t => t.Temperature);
                data.MaxTemperature = p.Max(t => t.Temperature);
                data.AvgTemperature = (Int32)p.Average(t => t.Temperature);
                data.MinFlow = p.Min(t => t.Flow);
                data.MaxFlow = p.Max(t => t.Flow);
                data.AvgFlow = p.Average(t => t.Flow);
                data.StartTime = Convert.ToDateTime(p.First().GenerateMinute);
                list.Add(data);
            }
            return list;
        }


        /// <summary>
        /// 通过id删除某条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteDataById(int id)
        {
            var result = _datadal.DeleteDataById(id);
            return result;
        }

        /// <summary>
        /// 通过model更新某条数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateDataByModel(SimDataModel model)
        {
            var result = _datadal.UpdateDataByModel(model);
            return result;
        }

        public bool DeleteDataByIdArray(int[] arrayId)
        {
            var result = _datadal.DeleteDataByIdArray(arrayId);
            return result;
        }

        public bool UpdataDataByModelList(List<SimDataModel> modellist)
        {
            var result = _datadal.UpdataDataByModelList(modellist);
            return result;
        }

    }
}
